﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;


namespace PlusTween
{
    public class PlusTweenSize : PlusTweenBase
    {
        public enum SetType
        {
            Default = 0,
            SizeAnchor = 1,
            InsetAndSizeEdge = 2,
        }


        public Vector2 from = Vector2.zero;
        public Vector2 to = Vector2.zero;

        public SetType m_setType = SetType.Default;
        
        public Vector2 inset = Vector2.zero;
        public RectTransform.Edge edge_width = RectTransform.Edge.Top;
        public RectTransform.Edge edge_height = RectTransform.Edge.Top;

        RectTransform rectTransform;
        RectTransform m_RectTransform
        {
            get {
                if (null == rectTransform)
                {
                    rectTransform = gameObject.GetComponent<RectTransform>();
                }
                return rectTransform;
            }
        }

        
        private Tweener tweener;
        private Vector2 value;
        private Vector2 TweenGetter() { return value; }
        private void TweenSetter(Vector2 _value) {
            value = _value;

            switch (m_setType)
            {
                case SetType.Default:
                    m_RectTransform.sizeDelta = value;
                    break;
                case SetType.SizeAnchor:
                    m_RectTransform.SetSizeWithCurrentAnchors( RectTransform.Axis.Horizontal, value.x );
                    m_RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, value.y);
                    break;
                case SetType.InsetAndSizeEdge:
                    m_RectTransform.SetInsetAndSizeFromParentEdge(edge_width, inset.x, value.x);
                    m_RectTransform.SetInsetAndSizeFromParentEdge(edge_height, inset.y, value.y);
                    break;
            }

           

          
        }

        private void OnEnable()
        {
            if (isBronOrigin) { SetValueFrom(); }
        }

        public override void StartIn()
        {
            if (null == tweener)
            {
                tweener = DOTween.To(TweenGetter, TweenSetter, to, duration);
                tweener.SetAutoKill(false);
            }
            tweener.ChangeValues(from, to, duration);
            tweener.Restart(true, 0);

            tweener.PlayForward();

        }

        public override void StartOut()
        {

            if (null == tweener)
            {
                tweener = DOTween.To(TweenGetter, TweenSetter, to, duration);
                tweener.SetAutoKill(false);
               
            }
            tweener.ChangeValues(to, from, duration);
            tweener.Restart(true, 0);

            tweener.PlayForward();
        }

        public override void SetValueFrom() { TweenSetter(from); }
        public override void SetValueTo() { TweenSetter(to); }

    }

}
